﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Net
Imports System.Globalization
Imports System.Data.Services.Client
Imports System
Imports Microsoft.Samples.WindowsPhoneCloud.StorageClient.Exceptions

Namespace Credentials

    Public Class StorageCredentialsSwtToken
        Implements IStorageCredentials

        Private Const ExpirationBuffer As Long = 5

        Private ReadOnly swtToken As String
        Private ReadOnly expiration As Long

        Public Sub New(ByVal authenticationToken As String, ByVal expiration As Long)
            Me.swtToken = authenticationToken
            Me.expiration = expiration
        End Sub

        Public Sub SignRequestLite(ByVal webRequest As WebRequest) Implements IStorageCredentials.SignRequestLite
            Me.ValidateToken()
            If webRequest Is Nothing Then
                Throw New ArgumentNullException("webRequest")
            End If

            webRequest.Headers("Authorization") = String.Format(CultureInfo.InvariantCulture, "OAuth {0}", Me.swtToken)
        End Sub

        Public Sub AddAuthenticationHeadersLite(ByVal requestData As RequestData, ByVal requestHeaders As WebHeaderCollection) Implements IStorageCredentials.AddAuthenticationHeadersLite
            Me.ValidateToken()
            If requestHeaders Is Nothing Then
                Throw New ArgumentNullException("requestHeaders")
            End If

            requestHeaders("Authorization") = String.Format(CultureInfo.InvariantCulture, "OAuth {0}", Me.swtToken)
        End Sub

        Public Sub SignRequest(ByVal webRequest As WebRequest, ByVal contentLength As Long) Implements IStorageCredentials.SignRequest
            Me.ValidateToken()
            If webRequest Is Nothing Then
                Throw New ArgumentNullException("webRequest")
            End If

            webRequest.Headers("Authorization") = String.Format(CultureInfo.InvariantCulture, "OAuth {0}", Me.swtToken)
        End Sub

        Private Sub ValidateToken()
            Dim epoch = (Date.UtcNow.Ticks - 621355968000000000) / 10000000
            If Me.expiration <= epoch + ExpirationBuffer Then
                Throw New SecurityTokenExpirationException("Your security token has expired. Please, log out the application and log in again.")
            End If
        End Sub
    End Class
End Namespace